package com.xiaoguang.selecttext

import android.view.View
import java.lang.ref.WeakReference


/**
 * author : wangbo
 * date   : 2023/11/23
 * desc   :
 */
object SelectHelper {

    private val selectWorkers = hashMapOf<View,WeakReference<SelectWorker>>()

    fun push(builder: SelectBuilder, selectListener: OnSelectListener?){
        val view = builder.mTextView
        val worker = builder.build()
        worker.setSelectListener(selectListener)
        selectWorkers[view] = WeakReference(worker)
    }

    fun offer(view: View):SelectWorker?{
        val savedWorkers = selectWorkers.toMutableMap()
        return savedWorkers[view]?.get()
    }

    fun startSelect(view:View){
        val savedWorkers = selectWorkers.toMutableMap()
        val otherWorkers = savedWorkers.filterNot { it.key == view }
        otherWorkers.mapValues { (_,v) -> v.get()?.resetSelect() }

        startSelectInternal(view)
    }

    fun startSelectInternal(view:View){
        val curWorker:SelectWorker? = offer(view)
        curWorker?.startSelect()
    }

    fun resetSelect(view: View){
        val curWorker = offer(view)
        curWorker?.resetSelect()
    }

    fun selectAll(view: View){
        val curWorker = offer(view)
        curWorker?.selectAll()
    }

    fun isPopShowing(view: View):Boolean{
        val curWorker = offer(view)
        return curWorker?.isPopShowing()?:false
    }

    fun destroy(view: View){
        val curWorker = offer(view)
        curWorker?.destroy()
    }

}